﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Management
{
    public partial class RoleManagement : CSMPageBase
    {
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                
                int Totals = TotalPageAmount("");
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
                BindMyData(0,"");
            } 
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex,string starts)
        {

            int skipAccount = PageIndex * 20;

            var MyQuery = (from a in db.TCsm_JS_Role where a.RoleName.StartsWith(starts) select a).Skip(skipAccount).Take(20);
            GridView_Roles.DataSource = MyQuery;
            GridView_Roles.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount(string starts)
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TCsm_JS_Role where a.RoleName.StartsWith(starts) select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            string id = txtUser.Text;
            int Totals = TotalPageAmount(id);



            int pages = (Totals % 20) == 0 ? (Totals / 20) : (Totals / 20 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx,id);

        }

        #endregion
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_RoleList.Visible = false;
            Panel_Edit.Visible = true;

            TextBox_Name.Text = "";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox_div('增加','Panel_Edit');</script>", false);

        }
         public int CreateId(int MaxNumber, int MinNumber)
        {
            Random MyId = new Random();
            int iResult;
            int iUp = MaxNumber;
            int iDown = MinNumber;
            iResult = MyId.Next(iDown, iUp);
            return iResult;

        }
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            TCsm_JS_Role myNewRow = new TCsm_JS_Role();
            int id = CreateId(999, 0);
            var query = from a in db.TCsm_JS_Role where a.RoleID == id select a;
            if (query.Count() == 0)
            {
                myNewRow.RoleID = id;
            }
            else
            {
            }
            myNewRow.RoleName = TextBox_Name.Text;
            db.TCsm_JS_Role.InsertOnSubmit(myNewRow);
            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");
                Panel_RoleList.Visible = true;
                Panel_Edit.Visible = false;
                BindMyData(0,"");
            }
            catch
            {


                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
            }
        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            TextBox_Name.Text = "";
            Panel_RoleList.Visible = true;
            Panel_Edit.Visible = false;
        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            Label_RoleInfo.Text = e.CommandArgument.ToString();

        }

        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            int myid = Convert.ToInt32(e.CommandArgument.ToString());
            string[] query = (from a in db.TCsm_JS_User select a.RoleID).ToArray();
            foreach (string a in query)
            {
                string[] temp = a.Split(',');
                if(temp.Contains(myid.ToString()))
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('该角色正在使用中不能删除'); </script>");
                    return;
                }
            }
            
            var myDelQuery = (from a in db.TCsm_JS_Role where a.RoleID == myid select a).Single();
            db.TCsm_JS_Role.DeleteOnSubmit(myDelQuery);

            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除成功'); </script>");
                BindMyData(0,"");

            }
            catch
            {


                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除失败'); </script>");
            }
        }





        protected void Button_Right_Command(object sender, CommandEventArgs e)
        {
            string myUserId = e.CommandArgument.ToString();
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox_aspx('RoleManagementSelect.aspx','" + myUserId + "','编辑角色');</script>", false);
        }
        protected void Button_Browse_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text;
            BindMyData(0, user);
        }
    }
}
